package zyy.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import zyy.model.BeanAdminUserinfo;
import zyy.model.BeanAdministrator;
import zyy.util.BaseException;
import zyy.util.BusinessException;
import zyy.util.DBUtil;
import zyy.util.DbException;
import zyy.util.Bus;

public class AdminManager {

  public BeanAdministrator login(String aName, String aPasswd) throws BaseException {
    if (aName == null || "".equals(aName) || aName.length() > 50) {
      throw new BusinessException("登陆账号 必须是 1~255 个字符");
    }
    if (aPasswd == null || "".equals(aPasswd) || aPasswd.length() > 50) {
      throw new BusinessException("密码 必须是 1~255 个字符");
    }
    BeanAdministrator result = null;
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "SELECT AdminID,aName,aPasswd from Administrator where aName=?";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setString(1, aName);
      java.sql.ResultSet rs = pst.executeQuery();
      if (rs.next()) {
        if (rs.getString(3).equals(aPasswd)) {
          result = new BeanAdministrator();
          result.setAdminID(rs.getInt(1));
          result.setaName(aName);
          result.setaPasswd(aPasswd);
        } else {
          throw new BusinessException("账号密码不匹配！");
        }
      } else {
        throw new BusinessException("查无此账号");
      }
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new DbException(e);
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
    return result;
  }

  public void reg(String aName, String pwd1, String pwd2) throws BaseException {
    if (aName == null || aName.equals("") || aName.length() > 255) {
      throw new BusinessException("昵称应为不大于255字符的非空字符串");
    }
    if (pwd1 == null || pwd1.equals("") || pwd1.length() > 255) {
      throw new BusinessException("密码应为不大于255字符的非空字符串");
    }
    if (!pwd1.equals(pwd2)) {
      throw new BusinessException("两次密码输入应该一致");
    }
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "SELECT a.AdminID FROM Administrator a WHERE aName = ?";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setString(1, aName);
      java.sql.ResultSet rs = pst.executeQuery();
      if (rs.next()) {
        String msg = String.format("当前账户 %s 与 ID:%d 的管理员重名了", aName, rs.getInt(1));
        throw new BusinessException(msg);
      } else {
        sql = "INSERT INTO Administrator(aName, aPasswd) values(?,?)";
        pst = conn.prepareStatement(sql);
        pst.setString(1, aName);
        pst.setString(2, pwd1);
        pst.executeUpdate();
      }
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new DbException(e);
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }

  }

  public BeanAdministrator changePasswd() throws BaseException {
    BeanAdministrator result = null;
    return result;
  }

  public List<BeanAdministrator> loadAllAdmin() throws BaseException {
    List<BeanAdministrator> result = null;
    return result;
  }

  public List<BeanAdminUserinfo> loadAllUser() throws BaseException {
    // userID,uName,loginName,uPasswd,uRegDate,[uOrders]
    // 视图建立语句
    // create VIEW viewAdminUser as
    // select u.UserID,uName,loginName,uPasswd,uRegDate,o.OrderID,o.oStatus
    // from Users u
    // left join Orders o on u.UserID=o.UserID
    Connection conn = null;
    List<BeanAdminUserinfo> result = new ArrayList<BeanAdminUserinfo>();
    try {
      conn = DBUtil.getConnection();
      String sql = " select v.UserID,v.uName,v.loginName,v.uPasswd,v.uRegDate,v.uVIP,v.uVIPtill,count(v.OrderID) OrderCnt"
          + " from viewadminuser v" + " GROUP BY v.UserID";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      java.sql.ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        BeanAdminUserinfo p = new BeanAdminUserinfo();
        p.setUserID(rs.getInt(1));
        p.setuName(rs.getString(2));
        p.setLoginName(rs.getString(3));
        p.setuPasswd(rs.getString(4));
        p.setuVIP(rs.getInt(6));
        // System.out.println("订单计数：" + rs.getInt(8));
        p.setOrderCnt(rs.getInt(8));
        if (rs.getString(5) != null)
          p.setuRegDate(Bus.sdf.parse(rs.getString(5)));
        else
          p.setuRegDate(null);
        if (rs.getString(7) != null)
          p.setuVIPtill(Bus.sdf.parse(rs.getString(7)));
        else
          p.setuVIPtill(null);
        result.add(p);
      }
      rs.close();
      pst.close();
    } catch (Exception e) {
      e.printStackTrace();
      throw new DbException(e);
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
    return result;
  }

  public static void main(String[] args) {
    try {
      (new AdminManager()).loadAllUser();
    } catch (BaseException e) {
      e.printStackTrace();
    }
  }
}